﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WoodManager.Header;
using Common;

namespace WoodManager.BO
{
    class ToHopVanSonBO
    {
        public string getToHopVanInfo(SqlConnection conn, ref DataTable tbTHVS)
        {
            string cmd = "Select * from ViewToHopVanSon";
            CommonDO cmDO = new CommonDO();
            return cmDO.getTable(conn, ref tbTHVS, cmd);
        }

        public String getInfo2Combobox(SqlConnection conn, ref DataTable tbVan, ref DataTable tbSon)
        {
            String ret = CommonHeader.NO_ERROR;
            String cmd = "Select * from VanVatLieu";
            CommonDO cmDO = new CommonDO();
            ret = cmDO.getTable(conn, ref tbVan, cmd);
            if (ret != CommonHeader.NO_ERROR)
            {
                return ret;
            }
            cmd = "Select * from SonPhu";
            ret = cmDO.getTable(conn, ref tbSon, cmd);
            return ret;                       
        }
        public String InsertRecord(SqlConnection conn, StructToHopVan stTHVS)
        {
            String cmd = "INSERT INTO ToHopVanSon (IDToHopSon, IDVan, IDSon, GhiChu) VALUES (" ;
            //set value to IDToHopSon
            cmd = cmd + "'" + stTHVS.sIDTopHopVanSon + "'" + ", ";
            //set value to IDVan
            cmd = cmd + "'" + stTHVS.sIDVan + "'" + ", ";
            //set value to IDSon
            cmd = cmd + "'" + stTHVS.sIDSon + "'" + ", ";
            //set GhiChu
            cmd = cmd + "'" + stTHVS.sGhiChu + "'" + ")";

            CommonDO cmDO = new CommonDO();
            return cmDO.ExcecuteQuery(conn, cmd);
        }

        public String UpdateRecord(SqlConnection conn, ref StructToHopVan stTHVS)
        {
            String cmd = "Update ToHopVanSon SET ";
            cmd = cmd + "IDVan = N'" + stTHVS.sIDVan + "', ";
            cmd = cmd + "IDSon = N'" + stTHVS.sIDSon + "', ";
            cmd = cmd + "GhiChu = N'" + stTHVS.sGhiChu + "'";
            cmd = cmd + "WHERE IDToHopSon = N'" + stTHVS.sIDTopHopVanSon + "'";

            CommonDO cmDO = new CommonDO();
            String ret = cmDO.ExcecuteQuery(conn, cmd);
            if (ret == CommonHeader.NO_ERROR)
            {
                stTHVS.sIDTopHopVanSon = stTHVS.sIDVan + stTHVS.sIDSon;
            }
            return ret;
        }

        public String DeleteRecord(SqlConnection conn, ref StructToHopVan stTHVS)
        { 
            String cmd = "DELETE ToHopVanSon WHERE IDToHopSon = N'" + stTHVS.sIDTopHopVanSon + "'";

            CommonDO cmDO = new CommonDO();
            String ret = cmDO.ExcecuteQuery(conn, cmd);
            if (ret == CommonHeader.NO_ERROR)
            {
                stTHVS.sIDTopHopVanSon = "";
                stTHVS.sIDSon = "";
                stTHVS.sIDVan = "";
                stTHVS.sGhiChu = "";
            }
            return ret;
        }
    }
}
